from scrapy import Request
from scrapy.spiders import Spider
from qidian_hot.items import QidianHotItem

class HotSalesSpider(Spider):
    name = "hot_sales"
    start_urls = ["https://www.qidian.com/rank/hotsales?style=1"]
    current_page = 1

    def parse(self, response):
        # 使用xpath定位
        list_selector = response.xpath("//div[@class='book-mid-info']")
        for one_selector in list_selector:
            # 获取小说名
            novel_name = one_selector.xpath("h4/a/text()").extract()[0]
            # 获取小说作者
            novel_author = one_selector.xpath("p[@class='author']/a[@data-eid='qd_C41']/text()").extract()[0]
            # 获取小说类型
            novel_type = one_selector.xpath("p[@class='author']/a[@data-eid='qd_C42']/text()").extract()[0]
            # 获取小说形式
            novel_form = one_selector.xpath("p[@class='author']/span/text()").extract()[0]

            item = QidianHotItem()
            item["name"] = novel_name
            item["author"] = novel_author
            item["type"] = novel_type
            item["form"] = novel_form
            yield item

            # 获取下一页URL，并生成一个Request请求
            self.current_page += 1
            if self.current_page <= 5:
                next_url = "https://www.qidian.com/rank/hotsales?style=1&page=%d" % self.current_page
                yield Request(next_url)

